package org.cuppaper.listener;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.block.Beehive;
import org.bukkit.event.block.Action;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;

public class Listener implements org.bukkit.event.Listener {
    //玩家加入事件
    @EventHandler
    void onPlayerJoin (PlayerJoinEvent event) {
        var player = event.getPlayer();
        event.setJoinMessage(ChatColor.GREEN + "玩家 " + ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " 加入了游戏！");
        for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
            onlinePlayer.getWorld().playSound(onlinePlayer.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
        }
    }
    
    @EventHandler
    //玩家退出事件
    void onPlayerQuit (PlayerQuitEvent event) {
        var player = event.getPlayer();
        event.setQuitMessage(ChatColor.RED + "玩家 " + ChatColor.YELLOW + player.getName() + ChatColor.RED + " 离开了游戏！");
        for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
            onlinePlayer.getWorld().playSound(onlinePlayer.getLocation(), Sound.ENTITY_ENDER_DRAGON_SHOOT, 1, 1);
        }
    }


    @EventHandler
    //检测Beehive蜜蜂数量以及蜂蜜量
    void getHoneycombInfo (PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getHand() == EquipmentSlot.OFF_HAND) {
            var block = event.getClickedBlock();
            if (block != null && !event.hasItem()) {
                var state = block.getState();
                if (state instanceof Beehive) {
                    var beeCounts = ((Beehive) state).getEntityCount();
                    var honeyLevels = ((org.bukkit.block.data.type.Beehive) block.getBlockData()).getHoneyLevel();
                    var beeCountText = new TextComponent("蜜蜂：");
                    beeCountText.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
                    var honeyLevelText = new TextComponent(" 蜂蜜：");
                    honeyLevelText.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
                    var beeCount = new TextComponent(String.valueOf(beeCounts));
                    beeCount.setColor(net.md_5.bungee.api.ChatColor.GREEN);
                    var honeyLevel = new TextComponent(String.valueOf(honeyLevels));
                    honeyLevel.setColor(net.md_5.bungee.api.ChatColor.GREEN);
                    var Beehivemsg = new TextComponent(beeCountText,beeCount,honeyLevelText,honeyLevel);
                    var player = event.getPlayer();
                    player.spigot().sendMessage(
                            ChatMessageType.ACTION_BAR,
                            Beehivemsg
                    );
                }
            }
        }
    }
}
